package cn.ee.dfs.websocket.controller;

import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.websocket.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * Description:
 * Created by TWJ on 2019/10/14 0014 0025 上午 9:38 .
 */
@Controller
public class SocketController {

    @GetMapping(DfsConstant.VIEW_PREFIX + "socket/home")
    public ModelAndView socket(ModelAndView mv){
        mv.setViewName(DfsConstant.VIEW_PREFIX + "socket/home");
        return mv;
    }

    //推送数据接口
    @ResponseBody
    @RequestMapping(DfsConstant.VIEW_PREFIX + "socket/push/{cid}")
    public DfsResponse pushToWeb(@PathVariable String cid, String message) {
        try {
            WebSocketServer.sendInfo(message,cid);
        } catch (IOException e) {
            e.printStackTrace();
            return new DfsResponse().fail().message(cid+"#"+e.getMessage());
        }
        return new DfsResponse().success().message(cid);
    }
}
